package com.zzh.takeoutapp.utils;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.base.BaseActivity;
import com.zzh.takeoutapp.view.AlwaysMarqueeTextView;


/**
 * 项目名称：
 * 类描述：
 * 创建人：
 * 创建时间：16/3/1 11:06
 * 修改人：Gophy
 * 修改时间：16/3/1 11:06
 * 修改备注：
 */
public class ToolBarHelper {

    private Context mContext;

    private FrameLayout mContentView;

    private View mUserView;

    private Toolbar mToolBar;

    private TextView tvTitle;


    private LayoutInflater mInflater;

    private static int[] ATTRS = {
            R.attr.windowActionBarOverlay,
            R.attr.actionBarSize
    };

    public ToolBarHelper(Context context, int layoutId) {
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        initContentView();
        initToolBar();
        initUserView(layoutId);
    }


    public ToolBarHelper(Context context, int layoutId, String title) {
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        initContentView();
        initToolBar();
        initUserView(layoutId, title);
    }

    private void initContentView() {
        /*直接创建一个帧布局，作为视图容器的父容器*/
        mContentView = new FrameLayout(mContext);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mContentView.setLayoutParams(params);

    }

    private void initToolBar() {
        /*通过inflater获取toolbar的布局文件*/
        View toolbar = mInflater.inflate(R.layout.toolbar, mContentView);
        mToolBar = (Toolbar) toolbar.findViewById(R.id.id_tool_bar);
        tvTitle = (TextView) toolbar.findViewById(R.id.tv_tool_bar);
        mToolBar.setTitle("");
//        mToolBar.setTitleTextColor(Color.parseColor("#FFFFFF"));
    }

    private void initUserView(int id) {
        mUserView = mInflater.inflate(id, null);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        TypedArray typedArray = mContext.getTheme().obtainStyledAttributes(ATTRS);
        /*获取主题中定义的悬浮标志*/
        boolean overly = typedArray.getBoolean(0, false);
        /*获取主题中定义的toolbar的高度*/
        int toolBarSize = (int) typedArray.getDimension((int) mContext.getResources().getDimension(R.dimen.abc_action_bar_default_height_material), 1);
        typedArray.recycle();
        /*如果是悬浮状态，则不需要设置间距*/
        params.topMargin = overly ? 0 : toolBarSize;
        mContentView.addView(mUserView, params);

    }

    private void initUserView(int id, String title) {
        mUserView = mInflater.inflate(id, null);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        TypedArray typedArray = mContext.getTheme().obtainStyledAttributes(ATTRS);
        /*获取主题中定义的悬浮标志*/
        boolean overly = typedArray.getBoolean(0, false);
        /*获取主题中定义的toolbar的高度*/
        int toolBarSize = (int) typedArray.getDimension((int) mContext.getResources().getDimension(R.dimen.abc_action_bar_default_height_material), 1);

        typedArray.recycle();
        /*如果是悬浮状态，则不需要设置间距*/
        params.topMargin = overly ? 0 : toolBarSize;

        tvTitle.setText(title);

        mContentView.addView(mUserView, params);

    }

    public static TextView setOnlyCenterTitle(BaseActivity activity, Toolbar toolbar, String resId) {
        activity.setSupportActionBar(toolbar);
        activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
        TextView subView = new TextView(activity);
        subView.setText(resId);
        subView.setTextSize(2, 18.0F);
        int colorWhite = activity.getResources().getColor(R.color.black);
        subView.setTextColor(colorWhite);
        toolbar.addView(subView);
        Toolbar.LayoutParams params = new Toolbar.LayoutParams(-2, -2, 17);
        subView.setLayoutParams(params);
        return subView;
    }

    public static void setBack(final BaseActivity activity, Toolbar toolbar) {
        if (toolbar != null) {
            activity.setSupportActionBar(toolbar);
            activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
            Drawable drawable = activity.getResources().getDrawable(R.mipmap.ic_launcher);
            toolbar.setNavigationIcon(drawable);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    activity.finish_Activity();
                }
            });
        }
    }

    public static void setNormalTileAndBackFragment(final BaseActivity activity,
                                                    Toolbar toolbar, String title, int menuId
            , final ToolBarHelper.MenuItemClickListener listener, View.OnClickListener onBackClickListener) {
        if (toolbar != null) {
            toolbar.setNavigationIcon(R.mipmap.ic_launcher);
            toolbar.setNavigationOnClickListener(onBackClickListener);
            if (menuId > 0) {
                toolbar.inflateMenu(menuId);
                toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        if (listener != null) {
                            listener.menuItemClick(item);
                        }
                        return true;
                    }
                });
            }

            setTitle(activity, toolbar, title);
        }

    }

    public static void setNormalTitle(final BaseActivity activity, Toolbar toolbar, String resId) {
        if (toolbar != null) {
            activity.setSupportActionBar(toolbar);
            activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
            setOnlyCenterTitle(activity, toolbar, resId);
        }
    }

    public static void setNormalTitleAndBack(final BaseActivity activity, int drawables, Toolbar toolbar, String resId) {
        if (toolbar != null) {
            activity.setSupportActionBar(toolbar);
            activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
            Drawable drawable = activity.getResources().getDrawable(R.drawable.ps_back_white);
            toolbar.setNavigationIcon(drawable);
            setOnlyCenterTitle(activity, toolbar, resId);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    activity.finish_Activity();
                }
            });
        }

    }


    public static void setNormalTitleAndBackFragment(final BaseActivity activity,
                                                     Toolbar toolbar, String resId, View.OnClickListener o) {
        if (toolbar != null) {
            activity.setSupportActionBar(toolbar);
            activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
            Drawable drawable = activity.getResources().getDrawable(R.mipmap.home_nextlevel_topnav_back);
            toolbar.setNavigationIcon(drawable);
            setOnlyCenterTitle(activity, toolbar, resId);
            toolbar.setNavigationOnClickListener(o);
        }
    }

    public static void setCommonTitleAndBackAndRight(final BaseActivity activity,
                                                     Toolbar toolbar, String title, int menuId
            , final ToolBarHelper.MenuItemClickListener listener) {
        if (toolbar != null) {
            toolbar.setNavigationIcon(R.mipmap.home_nextlevel_topnav_back);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    activity.finish();
                }
            });
            if (menuId > 0) {
                toolbar.inflateMenu(menuId);
                toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        if (listener != null) {
                            listener.menuItemClick(item);
                        }

                        return true;
                    }
                });
            }

            setTitle(activity, toolbar, title);
        }

    }


    public static void setTitleAndBackAndText(final BaseActivity activity, Toolbar toolbar, String title) {
        if (toolbar != null) {
            activity.setSupportActionBar(toolbar);
            activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
            Drawable drawable = activity.getResources().getDrawable(R.mipmap.home_nextlevel_topnav_back);
            toolbar.setNavigationIcon(drawable);
            toolbar.setNavigationContentDescription(R.string.ps_title_pic);
            setOnlyCenterTitle(activity, toolbar, title);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    activity.finish_Activity();
                }
            });
        }

    }


    public static void setImgTitleAndBack(final BaseActivity activity, Toolbar toolbar, int resId) {
        if (toolbar != null) {
            activity.setSupportActionBar(toolbar);
            activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
            Drawable drawable = activity.getResources().getDrawable(R.mipmap.home_nextlevel_topnav_back);
            toolbar.setNavigationIcon(drawable);
            toolbar.setNavigationContentDescription(R.string.ps_all_image);
            setImgTitle(activity, toolbar, resId);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    activity.finish_Activity();
                }
            });
        }
    }

    public static void setImgTitle(BaseActivity activity, Toolbar toolbar, int resId) {
        ImageView subView = new ImageView(activity);
        subView.setImageDrawable(activity.getResources().getDrawable(resId));
        subView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        toolbar.addView(subView);
        Toolbar.LayoutParams params = new Toolbar.LayoutParams(-2, -2, 49);
        subView.setLayoutParams(params);
    }


    public static void setTitle(BaseActivity activity, Toolbar toolbar, String title) {
        AlwaysMarqueeTextView subView = new AlwaysMarqueeTextView(activity);
        subView.setLayoutParams(new ViewGroup.LayoutParams(-2, -2));
        subView.setPadding(10, 0, 10, 0);
        subView.setSingleLine(true);
        subView.setEllipsize(TextUtils.TruncateAt.valueOf("MARQUEE"));
        subView.setMaxEms(8);
        subView.setMarqueeRepeatLimit(-1);
        subView.setHorizontallyScrolling(true);
        subView.setFocusableInTouchMode(true);
        subView.setText(title);
        subView.setTextSize(2, 20.0F);
        int colorWhite = activity.getResources().getColor(R.color.white);
        subView.setTextColor(colorWhite);
        toolbar.addView(subView);
        Toolbar.LayoutParams params = new Toolbar.LayoutParams(-2, -2, 49);
        subView.setLayoutParams(params);
    }


    public interface MenuItemClickListener {
        void menuItemClick(MenuItem var1);
    }

    public interface OnBackClickListener {
        void onBackClick();
    }


    public FrameLayout getContentView() {
        return mContentView;
    }

    public Toolbar getToolBar() {
        return mToolBar;
    }

}
